package com.example.back.controller.topup;

import com.example.back.bean.topup.DiamondsToCardsInfo;
import com.example.back.bean.topup.TopUpInfo;
import com.example.back.dao.topup.DiamondsToCardsDao;
import com.example.back.dao.topup.TopUpDao;
import com.example.back.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "配置充值信息接口")
@RestController
@RequestMapping("/config")
public class TopUpController {

    @Autowired
    private TopUpDao topUpDao;

    @Autowired
    private DiamondsToCardsDao diamondsToCardsDao;

    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation(value = "获取所有充值信息",httpMethod = "GET")
    @RequestMapping("topUpInfo")
    public List<TopUpInfo> getTopUpConfig(){
        return topUpDao.getTopUpConfig();
    }

    @ApiOperation(value = "配置充值信息",httpMethod = "PUT")
    @RequestMapping("/updateTopUp")
    public Map<String,Object> updateTopUp(int id,int money,int diamonds,int extra){
        Map<String,Object> map=  new HashMap<>();
        if(id==-1){
            int i = topUpDao.insertTopUp(money, diamonds, extra);
            System.out.println(i);
            map.put("code",1);
            return map;
        }
        int i = topUpDao.updateTopUp(id, money, diamonds, extra);
        System.out.println(i);
        if(i!=0){
            map.put("code",200);
            return map;
        }
        map.put("code",0);
        return map;
    }

    @ApiOperation(value = "删除充值配置",httpMethod = "DELETE")
    @RequestMapping("/deleteTopUpConfig")
    public Map<String,Object> deleteTopUpConfig(int id){
        Map<String,Object> map = new HashMap<>();
        int i = topUpDao.deleteTopUpInfo(id);
        System.out.println(i);
        if(i!=0){
            map.put("code",200);
            return map;
        }
        map.put("code",0);
        return map;
    }

    @ApiOperation(value = "获取钻石兑换金币配置信息",httpMethod = "GET")
    @RequestMapping("/diamondsToCards")
    public List<DiamondsToCardsInfo> getDiamondsToCardsInfo(){
        return diamondsToCardsDao.getDiamondsToCards();
    }

    @ApiOperation(value = "更新钻石兑换金币配置信息",httpMethod = "PUT")
    @RequestMapping("/updateDiamondsToCards")
    public Map<String,Object> updateDiamondsToCards(int id,int diamonds,int cards){
        Map<String,Object> map=  new HashMap<>();
        if(id==-1){
            int i = diamondsToCardsDao.insertDiamondsToCards(cards, diamonds);
            System.out.println(i);
            map.put("code",1);
            return map;
        }
        int i = diamondsToCardsDao.updateDiamondsToCards(id, cards, diamonds);
        System.out.println(i);
        if(i!=0){
            map.put("code",200);
            return map;
        }
        map.put("code",0);
        return map;
    }

    @ApiOperation(value = "删除钻石兑换金币配置信息",httpMethod = "DELETE")
    @RequestMapping("/deleteDiamondsToCards")
    public Map<String,Object> deleteDiamondsToCards(int id){
        Map<String,Object> map = new HashMap<>();
        int i = diamondsToCardsDao.deleteDiamondsToCards(id);
        System.out.println(i);
        if(i!=0){
            map.put("code",200);
            return map;
        }
        map.put("code",0);
        return map;
    }

    @RequestMapping("/robotConfig")
    public boolean robotConfig(String value){
        return redisUtils.set("robotInit", value);
    }

    @RequestMapping("/getRobotConfig")
    public Object getRobotConfig(){

        return redisUtils.get("robotInit");
    }

    @RequestMapping("/iosConfig")
    public boolean iosConfig(String value){
        return topUpDao.UpdateIosConfig(value);
    }

    @RequestMapping("/cgRewardConfig")
    public boolean cgRewardConfig(String value){
        return topUpDao.UpdateCgRewardConfig(value);
    }

    @RequestMapping("/getIosConfig")
    public String getIosConfig(){

        return topUpDao.selectIosConfig();
    }

    @RequestMapping("/getConfig")
    public Map<String,Object> getCgRewardConfig(){
        Map<String,Object> map = new HashMap<>();
        map.put("cgReward",topUpDao.selectCgRewardConfig());
        map.put("robotInit",redisUtils.get("robotInit"));
        map.put("ios",topUpDao.selectIosConfig());
        return map;
    }
}
